var ns, toast, actionsheet, UIAlbumBrowser;
var app = new Vue({
    el: '#app',
    data: {
        form:{
            ID:0,
            CouponType:2,
            CouponName:'',
            DiscountNum:'',
            DiscountMoney:'',
            PublishNumber:'',
            StartTime:'',
            EndTime:'',
            ReduceNum:'',
            Type:4,
            CategoryIDs:'',
            CategoryNames:'',
            ProductIDs:'',
            ProductNames:'',
            CategoryIDlist:[],
            CategoryNamelist:[],
            ProductIDlist:[],
            ProductNamelist:[],
            Remark:'',
            action:'savemallcouponcode'
        },
        list:[],
        fenleilist:[],
        showTime:false,
        youxiaoTime:'',
        showAll:true,
        showMenu:true,
    },
    methods: {
        get_data:function(){
            var that=this;
            var option={};
            option.ID=that.form.ID;
            option.action='getcouponinfo';
            ns.post(option, function(succeed, data, err) {
                console.log(JSON.stringify(err));
                console.log(JSON.stringify(data));
                if(succeed){
                    if(data.status){
                        that.form.CouponType=data.data.UseType;
                        if(that.form.CouponType==1){
                            that.form.DiscountMoney=data.data.CardMoneyDiscount;//面额
                        }else if(that.form.CouponType==2){
                            that.form.DiscountNum=data.data.CardMoneyDiscount;//折扣
                        }
                        that.form.CouponName=data.data.CouponName;
                        that.form.PublishNumber=data.data.UseNeedAmount;//发行数量
                        that.form.StartTime=data.data.StartTime;
                        that.form.EndTime=data.data.EndTime;
                        that.youxiaoTime=data.data.ValidTime;
                        that.form.ReduceNum=data.data.ReduceAmount;
                        that.form.Type=data.data.AmountType;
                        that.form.Remark=data.data.Summary;
                        if(that.form.Type==4){
                            that.fenleilist=data.data.AppointList;
                        }else if(that.form.Type==5){
                            that.list=data.data.AppointList;
                        }
                        

                    }
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        do_select_type:function(type){
            var that=this;
            that.form.CouponType=type;
        },
        do_select_fenlei:function(type){
            var that=this;
            that.form.Type=type;
        },
         //展开全部
         do_openall:function(){
            var that=this;
            that.showAll=!that.showAll;
        },
        do_opentype:function(){
            var that=this;
            that.showMenu=!that.showMenu;
        },
        //选择商品分类
        choose_fenlei:function(){
            var that=this;
            ns.openWin('product_sortChoose', '选择分类商品',{
                list:that.fenleilist
            });
        },

        //添加新活动
        add_product:function(){
            var that=this;
            ns.openWin('choose_product', '选择商品',{
                productType:1,
                list:that.list
            });
        },
        //删除商品
        updatenum:function(id){
            var that=this;
            that.list = that.list.filter((item) => {
                return item.ID != id
                });
        },
        //保存卡券
        do_saveProduct:function(){
            var that=this;
            if (!that.form.CouponName) {
                api.toast({
                    msg: '请输入卡券名称',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.form.CouponType == 2 && !that.form.DiscountNum) {
                api.toast({
                    msg: '请输入折扣',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.form.CouponType == 1 && !that.form.DiscountMoney) {
                api.toast({
                    msg: '请输入减免金额',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (!that.youxiaoTime) {
                api.toast({
                    msg: '请输入有效期',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.form.CouponType == 1 &&!that.form.ReduceNum) {
                api.toast({
                    msg: '请输入满减金额',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.form.Type == 4 && that.fenleilist.length<=0) {
                api.toast({
                    msg: '请添加商品分类',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.form.Type == 5 && that.list.length<=0) {
                api.toast({
                    msg: '请添加商品',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if(!that.form.PublishNumber){
                that.form.PublishNumber=0;
            }
            that.fenleilist.forEach((item)=>{
                that.form.CategoryIDlist.push(item.ID);
                that.form.CategoryNamelist.push(item.Name);
                that.form.CategoryIDs=JSON.stringify(that.form.CategoryIDlist);
                that.form.CategoryNames=JSON.stringify(that.form.CategoryNamelist);
            });
            that.list.forEach((item)=>{
                that.form.ProductIDlist.push(item.ID);
                that.form.ProductNamelist.push(item.Name);
                that.form.ProductIDs=JSON.stringify(that.form.ProductIDlist);
                that.form.ProductNames=JSON.stringify(that.form.ProductNamelist);
            });
            console.log(JSON.stringify(that.form));
            ns.post(that.form, function(succeed, data, err) {
                console.log(JSON.stringify(err));
                console.log(JSON.stringify(data));
                if(succeed){
                    if(data.status){
                        api.toast({
                            msg: '保存成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                        api.sendEvent({
                            name:'load_card'
                        });
                        setTimeout(() => {
                            api.closeWin();
                        }, 500);
                    }else{
                        api.toast({
                            msg: data.msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                   

                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
            
        },
        // 选择有效时间
        choose_time(){
            this.showTime=true;
        },

          onConfirm(date) {
            const [start, end] = date;
            this.youxiaoTime = this.formatDate(start) +'至'+ this.formatDate(end);
            this.form.StartTime=this.formatDate(start);
            this.form.EndTime=this.formatDate(end);
            this.showTime = false;
          },
          formatDate(day){
            var y = day.getFullYear();
            var m = day.getMonth() + 1;
            var d = day.getDate();
            var nowdate = y + '-' + (m >= 10 ? m : '0' + m) + '-' + (d >= 10 ? d : '0' + d);
            return nowdate;
        },
    }
       


      
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    // toast = new auiToast();
    // actionsheet = new auiActionsheet();
    UIAlbumBrowser = api.require('UIAlbumBrowser');
    app.form.ID = api.pageParam.id||0;
    if(app.form.ID){
        app.get_data();
    }
    api.addEventListener({
        name: 'do_add_product'
    }, function (ret, err) {
        console.log(JSON.stringify(ret.value.list));
        app.list = ret.value.list;
    });
    api.addEventListener({
        name: 'do_add_sortproduct'
    }, function (ret, err) {
        console.log(JSON.stringify(ret.value.list));
        app.fenleilist = ret.value.list;
    });

};
